import React from 'react'
import { connect } from 'dva'
import { Tabs } from 'antd'
import Header from './header'
import Application from './application'
import Modal from './modal'
import MyApply from './myApply'
import WaitCheck from './waitCheck'
import Checked from './checked'
import CheckedModal from './checkedModal'
import Details from './details'
import SliderDetails from './sliderDetails'

const TabPane = Tabs.TabPane
let keys = ''
const Printing = ({ location, dispatch, printing, loading }) => {
	const permissions = JSON.parse(sessionStorage.getItem("permissions"))
	const { userList, ImgList, details, goDetails, list, pagination, modalVisible, currentItem, checkModalVisible, menu, sliderData, workflowList } = printing
	// console.log(menu)
	// console.log(pagination)
	const applyList = params => {
		dispatch({
			type: 'printing/applyList',
			payload: { ...params }
		})
	}
	const lists = params => {
		dispatch({
			type: 'printing/query',
			payload: { ...params }
		})
	}
	const showMenu = id => {
		dispatch({
			type: 'printing/showMenu',
			payload: id
		})
	}
	const callback = key => {
		$("#sliderDetails").animate({ right: "-500" });
		keys = key
		const params = {
			status: key,
			// page:pagination.current,
			// pageSize:pagination.pageSize,
			page: 1,
			pageSize: 10,
		}
		const datas = {
			// page:pagination.current,
			// pageSize:pagination.pageSize,
			page: 1,
			pageSize: 10,
		}
		showMenu(key)
		if (key != 0) {
			applyList(params)
		} else {
			lists(datas)
		}

	}
	// const handleSearch = payload => {
	// 	dispatch({
	// 		type:"printing/query",
	// 		payload:{ ...payload }
	// 	})
	// }
	const headerProps = {
		userList,
		handleSearch(payload) {
			dispatch({
				type: "printing/query",
				payload: { ...payload }
			})
		}
	}
	const applicationProps = {
		dataSource: list,
		pagination,
		menu,
		onSearch(params) {
			dispatch({
				type: "printing/query",
				payload: { ...params }
			})
		},
		onChange(page) {
			dispatch({
				type: "printing/query",
				payload: {
					page: page.current,
					pageSize: page.pageSize
				}
			})
		},
		showModal(record) {
			dispatch({
				type: "printing/showModal",
				payload: { record }
			})
		},

	}
	const hideModal = () => {
		dispatch({
			type: "printing/hideModal"
		})
	}
	const hideCheckModal = () => {
		dispatch({
			type: "printing/hideCheckModal"
		})
	}
	const onOk = params => {
		dispatch({
			type: "printing/apply",
			payload: {
				...params,
				page: list.length == 1 && pagination.totalCount > 1 ? Number(pagination.current) - 1 : pagination.current,
				pageSize: pagination.pageSize,
			}
		})
	}
	const modalProps = {
		title: "档案打印",
		visible: modalVisible,
		currentItem,
		onCancel: hideModal,
		onOk: onOk,
	}
	const applyProps = {
		dataSource: list,
		pagination,
		menu,
		getWorkHistory(payload) {
			dispatch({
				type: "printing/getworkflowHistory",
				payload: { processInstanceId: payload.processInstanceId }
			})
		},
		onSearch(params) {
			dispatch({
				type: "printing/applyList",
				payload: { ...params }
			})
		},
		onChange(page) {
			dispatch({
				type: "printing/applyList",
				payload: {
					status: keys,
					page: page.current,
					pageSize: page.pageSize,
				}
			})
		},
		getAllImg(params) {
			dispatch({
				type: "printing/getAllImg",
				payload: {
					...params
				}
			})
		},
		goDetails(data) {
			dispatch({
				type: "printing/showDet",
				payload: {
					...data
				}
			})
		},
		getSliderDetails(record) {
			dispatch({
				type: "printing/getSliderDetails",
				payload: { record }
			})
		}
	}
	const onCheckOk = params => {
		dispatch({
			type: "printing/checked",
			payload: {
				...params,
			}
		})
	}
	const checkedModalProps = {
		title: "档案审核",
		visible: checkModalVisible,
		currentItem,
		onCancel: onCheckOk,
		onOk: onCheckOk,
		okText: "通过",
		cancelText: "拒绝",
	}
	const waitProps = {
		dataSource: list,
		pagination,
		menu,
		getWorkHistory(payload) {
			dispatch({
				type: "printing/getworkflowHistory",
				payload: { processInstanceId: payload.processInstanceId }
			})
		},
		onSearch(params) {
			dispatch({
				type: "printing/applyList",
				payload: { ...params }
			})
		},
		onChange(page) {
			dispatch({
				type: "printing/applyList",
				payload: {
					status: keys,
					page: page.current,
					pageSize: page.pageSize,
				}
			})
		},
		showCheckModal(record) {
			dispatch({
				type: "printing/showCheckModal",
				payload: { record }
			})
		},
		getSliderDetails(record) {
			dispatch({
				type: "printing/getSliderDetails",
				payload: { record }
			})
		}
	}
	const checkedProps = {
		dataSource: list,
		pagination,
		menu,
		getWorkHistory(payload) {
			dispatch({
				type: "printing/getworkflowHistory",
				payload: { processInstanceId: payload.processInstanceId }
			})
		},
		getSliderDetails(record) {
			dispatch({
				type: "printing/getSliderDetails",
				payload: { record }
			})
		},
		onSearch(params) {
			dispatch({
				type: "printing/applyList",
				payload: { ...params }
			})
		},
		onChange(page) {
			dispatch({
				type: "printing/applyList",
				payload: {
					status: keys,
					page: page.current,
					pageSize: page.pageSize,
				}
			})
		},
	}
	const detailsProps = {
		details,
		ImgList,
		showList() {
			dispatch({
				type: "printing/goList"
			})
		}
	}
	const SliderDetailsProps = {
		workflowList,
		sliderData,
		onCheckOk,
		menu,
		currentItem
	}
	return (
		<div>
			{goDetails == false ?
				<Tabs defaultActiveKey={menu} onChange={callback}>
					{permissions.indexOf("borrowManager:printing:apply") != -1 ?
						<TabPane tab="打印申请" key="0">
							<Header {...headerProps} />
							<Application {...applicationProps} />
							{modalVisible && <Modal {...modalProps} />}
						</TabPane>
						: ""}

					{permissions.indexOf("borrowManager:printing:apply") != -1 ?
						<TabPane tab="我的打印" key="1">
							<MyApply {...applyProps} />
						</TabPane>
						: ""}

					{permissions.indexOf("borrowManager:printing:checked") != -1 ?
						<TabPane tab="待审核" key="2">
							<WaitCheck {...waitProps} />
							{checkModalVisible && <CheckedModal {...checkedModalProps} />}
						</TabPane>
						: ""}

					{permissions.indexOf("borrowManager:printing:checked") != -1 ?
						<TabPane tab="已审核" key="3">
							<Checked {...checkedProps} />
						</TabPane>
						: ""}
				</Tabs> :
				<Details {...detailsProps} />
			}
			<SliderDetails {...SliderDetailsProps} />
		</div>
	)
}
export default connect(({ printing, loading }) => ({ printing, loading }))(Printing)
